package org.game.quartz;

import com.google.common.collect.Lists;
import com.sojoys.artifact.manager.MessageManager;
import com.xiaoleilu.hutool.date.DateUtil;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import org.game.cache.OnlinePlayerCache;
import org.game.db.bean.Player;
import org.game.db.bean.Role;
import org.game.module.rolebo.RoleBO;
import org.game.msgBuilder.RoleMsgBuilder;
import org.game.util.ProtoUnit;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.Collection;
import java.util.List;

/**
 * 更新看板娘心情值 Created by fxf on 2017/11/9.
 */
public class UpdateRoleMoodJob implements Job {

	static final Log log = LogFactory.get(UpdateRoleMoodJob.class);

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			Collection<Player> players = OnlinePlayerCache.getAll();
			updateMood(players);

		} catch (Exception e) {
			log.error(e);
		}

	}

	private void updateMood(Collection<Player> players) {
		boolean isZeroMinute = DateUtil.thisMinute() == 0;
		players.forEach(player -> {
			List<Role> updateRoles = Lists.newArrayList();
			List<Role> roles = player.getRolesFkPlayerId();
			roles.forEach(role -> {
				if (role.getRoleProxy().updateMood()) {
					updateRoles.add(role);
				}

				if (isZeroMinute && role.getRoleProxy().addExtraMood()){
					updateRoles.add(role);
				}
			});
			player.getPlayerProxy().sendMsg(MessageManager.me().create(RoleBO.updateRoleMood,
					ProtoUnit.toByte(RoleMsgBuilder.createRoleMoodList(updateRoles))));
		});

	}
}
